<template>
<section>
  <el-row>
    <el-col :span="6">
      <el-input placeholder="请输入内容" v-model="searchKey" prefix-icon="el-icon-fa-search">
        <div slot="append">
          <el-button type="primary" icon="el-icon-fa-search" @click=" pager.pager.pageNumber = 1 ;doSearch()"></el-button>
        </div>
      </el-input>
    </el-col>
    <el-col :span="6" :offset="12">
      <el-button type="primary" icon="el-icon-fa-plus" @click="addManor" size="small">添加庄园</el-button>
    </el-col>
  </el-row>
  <el-table :data="pager.dataList" border stripe style="width: 100%">
    <el-table-column prop="id" label="ID" header-align="center" align="center" width="55">
    </el-table-column>
    <el-table-column prop="name" label="名称" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="type" label="品种" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="level" label="等级" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="flavor" label="风味" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="mode" label="处理方式" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column label="操作" show-overflow-tooltip header-align="center" align="center">
      <template slot-scope="scope">
        <el-button-group>
          <el-button title="编辑庄园" size="mini" type="primary" icon="el-icon-fa-edit" @click="handleEdit(scope.$index,scope.row)"></el-button>
          <el-button title="编辑庄园" size="mini" type="primary" icon="el-icon-fa-trash" @click="handleDelete(scope.$index,scope.row)"></el-button>
        </el-button-group>
      </template>
    </el-table-column>
  </el-table>
  <!--分页条 -->
  <el-row>
    <el-col :span="6" :offset="18">
      <el-pagination background small style="float:right" layout="prev, pager, next" :total="pager.pager.recordCount" :page-size="pager.pager.pageSize" :current-page.sync="pager.pager.pageNumber" v-show="pager.pager.pageCount != 0" @current-change="changePage">
      </el-pagination>
    </el-col>
  </el-row>
  <!-- 弹框区域-->
  <el-dialog :title="manor.id == 0 ? '添加庄园' : '编辑庄园' " :visible.sync="addEditShow" width="50%">
    <el-form :model="manor" :rules="$rules.manor" ref="manorForm">
      <el-form-item label="产区" :label-width="formLabelWidth" prop="areaId">
        <el-select v-model="manor.areaId" filterable  placeholder="请选择产区">
          <el-option v-for="item in areas" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="庄园名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="manor.name" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写庄园名称"></el-input>
      </el-form-item>
      <el-form-item label="种植品种" :label-width="formLabelWidth" prop="type">
        <el-input v-model="manor.type" auto-complete="off" suffix-icon="el-icon-fa-text-height" placeholder="请填写庄园种植品种"></el-input>
      </el-form-item>
      <el-form-item label="采摘时间" :label-width="formLabelWidth" prop="pickingTime">
        <el-input v-model="manor.pickingTime" auto-complete="off" suffix-icon="el-icon-fa-shower" placeholder="请填写采摘时间"></el-input>
      </el-form-item>
      <el-form-item label="等级" :label-width="formLabelWidth" prop="level">
        <el-input v-model="manor.level" auto-complete="off" suffix-icon="el-icon-fa-thermometer-full" placeholder="请填写庄园原料等级"></el-input>
      </el-form-item>
      <el-form-item label="风味" :label-width="formLabelWidth" prop="flavor">
        <el-input v-model="manor.flavor" auto-complete="off" suffix-icon="el-icon-fa-thermometer-full" placeholder="请填写风味"></el-input>
      </el-form-item>

      <el-form-item label="处理方式" :label-width="formLabelWidth" prop="mode">
        <el-input v-model="manor.mode" auto-complete="off" suffix-icon="el-icon-fa-trello" placeholder="请填写处理方式"></el-input>
      </el-form-item>

      <el-form-item label="庄园详情" :label-width="formLabelWidth" prop="description">
        <editor id="editor_id" height="500px" width="700px" :content="manor.description" :extraFileUploadParams="params" :uploadJson="imgupload" :afterChange="afterChange()" pluginsPath="/static/kindeditor/plugins/" :loadStyleMode="false" @on-content-change="onContentChange"></editor>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="addEditShow = false ; manor = {id:0}">取 消</el-button>
      <el-button type="primary" @click="saveOrUpdateManor('manorForm')">确 定</el-button>
    </div>
  </el-dialog>
</section>
</template>

<style>
</style>

<script>
import config from "../../common/config";
export default {
  data() {
    return {
      imgupload: config.http.baseUrl + "image/kind/upload",
      searchKey: "",
      params: {
        base: config.http.baseUrl
      },
      pager: {
        pager: {
          pageCount: 1,
          pageNumber: 1,
          pageSize: 15,
          recordCount: 1
        },
        dataList: []
      },
      addEditShow: false,
      formLabelWidth: "100px",
      manor: {
        id: 0
      },
      areas: []
    };
  },
  methods: {
    loadData() {
      this.$api.Manor.list(this.pager.pager.pageNumber, result => {
        this.pager = result.pager;
      });
    },
    addManor() {
      this.addEditShow = true;
      this.manor = {
        id: 0
      };
    },
    handleEdit(index, row) {
      this.manor = row;
      this.addEditShow = true;
    },
    handleDelete(index, row) {
      this.$confirm("确认删除庄园?", "删除确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$api.Manor.delete(row.id, result => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          window.setTimeout(() => {
            if (this.searchKey) {
              this.doSearch();
            } else {
              this.loadData();
            }
          }, 2000);
        });
      });
    },
    changePage() {
      if (this.searchKey) {
        this.doSearch();
      } else {
        this.loadData();
      }
    },
    doSearch() {
      this.$api.Manor.search(
        this.pager.pager.pageNumber,
        this.searchKey,
        result => {
          this.pager = result.pager;
        }
      );
    },
    saveOrUpdateManor(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var callback = result => {
            this.changePage();
            this.addEditShow = false;
          };
          this.manor.id
            ? this.$api.Manor.update(this.manor, callback)
            : this.$api.Manor.save(this.manor, callback);
        } else {
          return false;
        }
      });
    },
    onContentChange(val) {
      this.manor.description = val;
    },
    afterChange() {}
  },
  created() {
    this.$api.Manor.areas(result => {
      this.areas = result.areas;
      this.loadData();
    });
  }
};
</script>
